package com.pillpoppers.pillbox;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.pillpoppers.pillbox.database.Pill;
import com.pillpoppers.pillbox.database.pillDB;

public class AlarmExecutionActivity extends Activity implements OnClickListener {
	
	private ArrayAdapter<String> adapter;
	private PowerManager.WakeLock wakeLock;
	private Ringtone ringtone;
	private int alarmBy;
	private ArrayList<Pill> pills;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_execution);
		
		prepareWakeLock();
		prepareAlarmBy();
		
		
	//	ArrayList<Integer> pillIds = getIntent().getIntegerArrayListExtra("pillIds");
		/*for (int pillId : pillIds) {
			pills.add(db.getPill(pillId));
		}*/

		initAdapter();
		initViews();
		
		setKeyguardFlags();
	}
	
	private void prepareWakeLock() {
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(
				PowerManager.SCREEN_BRIGHT_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP
				| PowerManager.ON_AFTER_RELEASE, getClass().getName());
		wakeLock.acquire(180 * 1000); // milliseconds passed as argument + duration set in device
	}
	
	private void prepareAlarmBy() {
		alarmBy = getIntent().getIntExtra("alarmBy", Schedule.ALARM_BY_SILENT);
		
		if (alarmBy == Schedule.ALARM_BY_VIBRATE) {
			long[] pattern = {0, 400, 500, 400, 500, 400, 500, 400, 500, 400, 500};
			
			Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			vibrator.vibrate(pattern, -1);
		} else if (alarmBy == Schedule.ALARM_BY_RING) {
			Uri uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
			if (uri == null) {
				uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
			}
			ringtone = RingtoneManager.getRingtone(this, uri);
			ringtone.play(); 
		}
	}
	
	private void initAdapter() {
		pillDB db = new pillDB(this);
		pills = db.getPills();
		ArrayList<String> data = new ArrayList<String>();
		for (int i = 0; i < pills.size(); i++)
			data.add(pills.get(i).getName());
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
	}
	
	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.listview);
		listView.setAdapter(adapter);

		findViewById(R.id.dismiss_btn).setOnClickListener(this);
	}
	
	private void setKeyguardFlags() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, 
				WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD, 
				WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.dismiss_btn) {
			if (alarmBy == Schedule.ALARM_BY_VIBRATE) {
				Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
				vibrator.cancel();
			} else if (alarmBy == Schedule.ALARM_BY_RING) {
				ringtone.stop();
			}
			
			wakeLock.release();
			finish();
		} else {
			
		}
	}

}
